//
//  AppDelegate.m
//  OCKSFWeibo
//
//  Created by 盛 发 况 on 16/4/15.
//  Copyright © 2016年 盛 发 况. All rights reserved.
//

#import "AppDelegate.h"
#import "CZTabBarController.h"
#import "CZNewFeatureController.h"
#import <AVFoundation/AVFoundation.h>
#define CZVersionKey  @"Bundle version"

#import <UIImageView+WebCache.h>

@interface AppDelegate ()
@property (nonatomic, strong) AVAudioPlayer *player;

@end

@implementation AppDelegate
//Bundle version
//CFBundleVersion

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    

    //选择加载控制器
    [self chooseViewController];
    
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

//选择加载控制器
- (void)chooseViewController {

    // 1.获取当前的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    // 2.获取上一次的版本号
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults]objectForKey:CZVersionKey];
    
    if ([currentVersion isEqualToString:lastVersion]) {
        CZTabBarController *tabBarVc = [[CZTabBarController alloc] init];
        self.window.rootViewController = tabBarVc;
        
    } else {
        CZNewFeatureController *newFeatureVC = [[CZNewFeatureController alloc] init];
        self.window.rootViewController = newFeatureVC;
        
        [[NSUserDefaults standardUserDefaults]setObject:currentVersion forKey:CZVersionKey];
    }
}

// 接收到内存警告的时候调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

    // 停止所有的下载
    [[SDWebImageManager sharedManager] cancelAll];
    
    // 删除缓存
    [[SDWebImageManager sharedManager].imageCache clearMemory];
    
}

// 失去焦点
- (void)applicationWillResignActive:(UIApplication *)application {
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"silence.mp3" withExtension:nil];
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [player prepareToPlay];
    
    // 无限播放
    player.numberOfLoops = -1;
    [player play];
    
    _player = player;
}

// 程序进入后台的时候调用
- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    
    // 开启一个后台任务,时间不确定，优先级比较低，假如系统要关闭应用，首先就考虑
  UIBackgroundTaskIdentifier ID = [application beginBackgroundTaskWithExpirationHandler:^{
      
      // 当后台任务结束的时候调用
      [application endBackgroundTask:ID];
  }];
    
    // 如何提高后台任务的优先级，欺骗苹果，我们是后台播放程序
    
    // 但是苹果会检测你的程序当时有没有播放音乐，如果没有，有可能就干掉你
    
    // 微博：在程序即将失去焦点的时候播放静音音乐.
    
    
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
